﻿using Microsoft.Phone.Tasks;
using Microsoft.Practices.Prism.Commands;
using Sinergija13.DataModel.Models.Conf;
using Sinergija13.DataServices.Store.Conf;
using Sinergija13.WP7App.Services;
using Sinergija13.WP7App.Themes;
using System;
using System.Collections.Generic;
using System.Device.Location;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace Sinergija13.WP7App.ViewModels
{
    public class EventInfoViewModel : ViewModel
    {
        private readonly IConfDataStoreLocator confDataStoreLocator; // ctor param
        public DelegateCommand BingMapCommand { get; set; }

        public EventInfoViewModel(
            IConfDataStoreLocator confDataStoreLocator,
            INavigationService navigationService)
            : base(navigationService)
        {
            this.confDataStoreLocator = confDataStoreLocator;
            initialize();
        }

        private void initialize()
        {
            this.BingMapCommand = new DelegateCommand(
            () =>
            {
                BingMapsTask bingMapsTask = new BingMapsTask();
                bingMapsTask.Center = new GeoCoordinate(Event.Latitude, Event.Longitude);
                bingMapsTask.SearchTerm = Event.Address;
                bingMapsTask.ZoomLevel = 2;

                bingMapsTask.Show();
            });
        }

        private int? id;
        private Event _event;
        public Event Event
        {
            get
            {
                if (this.id == null)
                {
                    var id = int.Parse(NavigationService.CurrentSource.OriginalString.Split('=')[1]);
                    this.id = id;
                    _event = confDataStoreLocator.GetStore().FindEvent(id);
                }

                return _event;
            }
        }

        public string Name { get { return Event.Name; } }
        public string TimeSpanString { get { return Event.TimeSpanString; } }
        public string Place { get { return Event.Place; } }
        public string Address { get { return Event.Address ?? "-"; } }
        public string Description { get { return Event.Description ?? "-"; } }
        public string Note { get { return Event.Note ?? "-"; } }
        
        public BitmapImage PushpinImage
        {
            get
            {
                return new BitmapImage(new Uri(
                    ThemeLocator.LightThemeEnabled ? 
                    "/Resources/Icons/Light/feature.pushpin.png" :
                    "/Resources/Icons/Dark/feature.pushpin.png", UriKind.RelativeOrAbsolute));
            }
        }

        public bool HasMap
        {
            get
            {
                return Event.Latitude != 0.0 && Event.Address != null;
            }
        }

        public override void IsBeingActivated()
        {}
    }
}
